﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;




namespace ZG_member
{
    public partial class ConsumeSolutionSettingForm : Form
    {
        ConsumeSolutionForm Form;
        public ConsumeSolutionSettingForm(ConsumeSolutionForm For)
        {
            InitializeComponent();
            Form = For;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }
        public static string Values;
        public static string TempName;
        public static string taocanxiangqing;
        public static string taocanxiangqingMoney;
        private void ConsumeSolutionSettingForm_Load(object sender, EventArgs e)
        {
            
            this.timer1.Start();
            if (Form.ID != 0)
            {
               this.Text = "修改消费方案";
            }
            moneyForSale.Text = "1";
            integralTxt.Text = "1";
            moneyForSaving.Text = "1";
            addMoney.Text = "1";
            CardType();
            cardStyleTemplate();
            
          
        }
        private void CardType()
        {
            consumeSolution consume = new consumeSolution();
            DataTable dt = consume.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!cbxnextlevelid.Items.Contains(dt.Rows[i]["solutionName"].ToString()))
                {
                    cbxnextlevelid.Items.Add(dt.Rows[i]["solutionName"].ToString());


                }
            }
            cbxnextlevelid.Items.Add("最高");
            for (int i = 0; i < cbxnextlevelid.Items.Count; i++)
            {
                string itemName = cbxnextlevelid.Items[i].ToString();
                if (Values == "0")
                {

                    cbxnextlevelid.SelectedItem = "最高";
                    return;
                }
                if (itemName == Values)
                {
                    cbxnextlevelid.SelectedIndex = i;
                    return;
                }
            }
        }
        private void cardStyleTemplate()
        {
            //cardTemplates card = new cardTemplates();
            //DataTable dt = card.GetList("").Tables[0];
            ////for (int i = 0; i < dt.Rows.Count; i++)
            ////{
            ////    if (!cardStyleTemplateCbx.Items.Contains(dt.Rows[i]["name"].ToString()))
            ////    {
            ////        cardStyleTemplateCbx.Items.Add(dt.Rows[i]["name"].ToString());
            ////    }
            ////}

            //cardStyleTemplateCbx.DataSource = dt;
            //cardStyleTemplateCbx.DisplayMember = "name";
            //cardStyleTemplateCbx.ValueMember = "id";
            //if (Form.ID != 0)
            //{
            //    cardStyleTemplateCbx.SelectedValue = TempName;
            //}
            cardTemplates consume = new cardTemplates();
            DataTable dt = consume.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!cardStyleTemplateCbx.Items.Contains(dt.Rows[i]["name"].ToString()))
                {
                    cardStyleTemplateCbx.Items.Add(dt.Rows[i]["name"].ToString());


                }
            }
            for (int i = 0; i < cardStyleTemplateCbx.Items.Count; i++)
            {
                string itemName = cardStyleTemplateCbx.Items[i].ToString();
                if (itemName == TempName)
                {
                    cardStyleTemplateCbx.SelectedIndex = i;
                    return;
                }
            }
        }

        private void btnremovesale_Click(object sender, EventArgs e)
        {
            if (saleBox.SelectedIndex != -1)
            {
                saleBox.Items.RemoveAt(saleBox.SelectedIndex);
            }
        }

        private void btnaddsaving_Click(object sender, EventArgs e)
        {
            if (moneyForSaving.Text == string.Empty || addMoney.Text == string.Empty)
            {
                MessageBox.Show("请填写完整信息!");
                return;
            }

            savingBox.Items.Add(moneyForSaving.Text + "," + addMoney.Text);
        }

        private void btnremovesaving_Click(object sender, EventArgs e)
        {
            if (savingBox.SelectedIndex != -1)
            {
                savingBox.Items.RemoveAt(savingBox.SelectedIndex);
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (methodNameTxt.Text == string.Empty)
            {
                MessageBox.Show("方案名不能为空!");
                return;
            }
            if (saleBox.Items.Count == 0)
            {

                MessageBox.Show("必需要有一个消费方案!");
                return;
            }
            if (savingBox.Items.Count == 0)
            {
                MessageBox.Show("必需要有一个充值方案!");
                return;

            }
            if (disconntMethodTxt.Text == string.Empty)
            {
                MessageBox.Show("折扣方案不能为空!");
                return;
            } 
            if (Convert.ToDouble(disconntMethodTxt.Text) <= 0)
            {
                MessageBox.Show("折扣方案必须大于0!");
                return;
            }
            if (txtleaveUp.Text == string.Empty)    
            {
                MessageBox.Show("升级条件不能为空!");
                return;
            }
            if (cbxnextlevelid.Text == string.Empty)
            {
                MessageBox.Show("下一等级不能为空!");
                return;
            }

            consumeSolution solution = new consumeSolution();
            Model.consumeSolution solutionModel = new Model.consumeSolution();
            if( this.Text == "修改消费方案")
            {
                
                solutionModel = solution.GetModelList("id='" + Form.ID + "'")[0];
            }

            solutionModel.solutionName = methodNameTxt.Text;
            cardTemplates card = new cardTemplates();
            Model.cardTemplates cardmodel = new Model.cardTemplates();
            cardmodel = card.GetModelList("name='" + cardStyleTemplateCbx.Text + "'")[0];
            solutionModel.cardTemplateId = cardmodel.id;
            solutionModel.money = Convert.ToDouble(taocanxiangqingMoney);
            bool cuxiaozhekou = true;
            if (this.radioButton1.Checked == false)
            {
                cuxiaozhekou = false;
            }
            int leaveDate1 = Convert.ToInt32(this.numericUpDown1.Value);
            int leaveDate2 = Convert.ToInt32(this.numericUpDown2.Value);
            int leaveDate3 = 0;
            if (leaveDate1 > leaveDate2)
            {
                leaveDate3 = leaveDate1;
                leaveDate1 = leaveDate2;
                leaveDate2 = leaveDate3;
            }
            solutionModel.leaveDate = leaveDate1 + "-" + leaveDate2;
            solutionModel.cuxiaozhekou = cuxiaozhekou;
            solutionModel.Taocanxiangqing = taocanxiangqing;
            string saleMethod = string.Empty;
            for (int i = 0; i < saleBox.Items.Count; i++)
            {
                saleMethod += saleBox.Items[i].ToString() + "|";
            }
            solutionModel.consumeSolution1 = saleMethod;
            string savingMethod = string.Empty;

            for (int i = 0; i < savingBox.Items.Count; i++)
            {
                savingMethod += savingBox.Items[i].ToString() + "|";
            }
            solutionModel.savingSolution = savingMethod;
            solutionModel.discountSolution = disconntMethodTxt.Text;
            solutionModel.Integralday =Convert.ToInt32(this.textBox1.Text);
            //if (cbxnextlevelid.Text.Trim() != "已是最高等级")
            //{
            //    consumeSolution con = new consumeSolution();
            //    Model.consumeSolution conMod = new Model.consumeSolution();
            //    conMod = con.GetModelList("solutionName='" + cbxnextlevelid.Text.Trim() + "'")[0];
            //    solutionModel.nextLevelId = conMod.id;
            //}
            //else
            //{
            //    solutionModel.nextLevelId = 0;
            //}
            if(cbxnextlevelid.Text=="最高")
            {
                solutionModel.nextLevelId = 0;
            }
            else
            {
                consumeSolution con = new consumeSolution();
                Model.consumeSolution conMod = new Model.consumeSolution();
                conMod = con.GetModelList("solutionName='" + cbxnextlevelid.Text.Trim() + "'")[0];
                solutionModel.nextLevelId = conMod.id;
            }
            if (txtleaveUp.Text != "")
            {
                solutionModel.levelUp = decimal.Parse(txtleaveUp.Text);
            }
            if (this.Text == "修改消费方案")
            {
                if (solution.Update(solutionModel))
                {
                    MessageBox.Show("修改成功!");
                    Form.dgvConsumeSolutionBind();

                    this.Close();
                    

                    
                }
            }
            else
            {
                int row = solution.Add(solutionModel);
                if (row > 0)
                {

                    MessageBox.Show("添加成功!");
                    Form.dgvConsumeSolutionBind();
                    this.Close();
                }
            }
            
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void ConsumeSolutionSettingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (methodNameTxt.Text == string.Empty)
            {
                MessageBox.Show("方案名不能为空!");
                return;
            }
            if (saleBox.Items.Count == 0)
            {

                MessageBox.Show("必需要有一个消费方案!");
                return;
            }
            if (savingBox.Items.Count == 0)
            {
                MessageBox.Show("必需要有一个充值方案!");
                return;

            }
            if (disconntMethodTxt.Text == string.Empty)
            {
                MessageBox.Show("折扣方案不能为空!");
                return;
            } if (Convert.ToDouble(disconntMethodTxt.Text) <= 0)
            {
                MessageBox.Show("折扣方案必须大于0!");
                return;
            }
            //if (txtleaveUp.Text == string.Empty)
            //{
            //    MessageBox.Show("升级条件不能为空!");
            //    return;
            //}
            //if (cbxnextlevelid.Text == string.Empty)
            //{
            //    MessageBox.Show("下一等级不能为空!");
            //    return;
            //}
            consumeSolution solution = new consumeSolution();
            Model.consumeSolution solutionModel = new Model.consumeSolution();
            if (this.Text == "修改消费方案")
            {

                solutionModel = solution.GetModelList("id='" + Form.ID + "'")[0];
            }
            solutionModel.solutionName = methodNameTxt.Text;
            cardTemplates card = new cardTemplates();
            Model.cardTemplates cardmodel = new Model.cardTemplates();
            cardmodel = card.GetModelList("name='" + cardStyleTemplateCbx.Text + "'")[0];
            solutionModel.cardTemplateId = cardmodel.id;


            string saleMethod = string.Empty;
            for (int i = 0; i < saleBox.Items.Count; i++)
            {
                saleMethod += saleBox.Items[i].ToString() + "|";
            }
            solutionModel.consumeSolution1 = saleMethod;
            string savingMethod = string.Empty;

            for (int i = 0; i < savingBox.Items.Count; i++)
            {
                savingMethod += savingBox.Items[i].ToString() + "|";
            }
            solutionModel.savingSolution = savingMethod;
            solutionModel.discountSolution = disconntMethodTxt.Text;
            solutionModel.Integralday = Convert.ToInt32(this.textBox1.Text);
            if (cbxnextlevelid.Text.Trim() != "已是最高等级")
            {
                consumeSolution con = new consumeSolution();
                Model.consumeSolution conMod = new Model.consumeSolution();
                conMod = con.GetModelList("solutionName='" + cbxnextlevelid.Text.Trim() + "'")[0];
                solutionModel.nextLevelId = conMod.id;
            }
            else
            {
                solutionModel.nextLevelId = 0;
            }
            if (txtleaveUp.Text != "")
            {
                solutionModel.levelUp = decimal.Parse(txtleaveUp.Text);
            }
            if (this.Text == "修改消费方案")
            {
                if (solution.Update(solutionModel))
                {
                    MessageBox.Show("修改成功!");
                    Form.dgvConsumeSolutionBind();
                    this.Close();




                }
            }
            else
            {
                int row = solution.Add(solutionModel);
                if (row > 0)
                {

                    MessageBox.Show("添加成功!");
                    Form.dgvConsumeSolutionBind();
                    this.Close();
                }
            }
        }

        private void btnAddsale_Click(object sender, EventArgs e)
        {
            if (moneyForSale.Text == string.Empty || integralTxt.Text == string.Empty)
            {
                MessageBox.Show("请填写完整信息!");
                return;
            }

            saleBox.Items.Add(moneyForSale.Text + "," + integralTxt.Text);
        }

        private void moneyForSale_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void integralTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void moneyForSaving_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void addMoney_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void disconntMethodTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void txtleaveUp_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            TaoCanfangan2 cf = new TaoCanfangan2(Form.ID);
            cf.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            MessageBox.Show("Test");
        }

        

       




    }
}